/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for 
 * full list of contributors). Published under the Clear BSD license.  
 * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */

/**
 * Class: OpenLayers.LocalTileStorage.TileWebStorage
 * Class for access to SQL Web Storage or Web (local) Storage.
 * Allows you to store, retrieve and get information about what
 * is stored. 
 * 
 * Inherits from:
 *  - <OpenLayers.LocalTileStorage>
 */

/**
 * @requires OpenLayers/Layer.js
 */

OpenLayers.LocalTileStorage.TileWebStorage =  OpenLayers.Class(OpenLayers.LocalTileStorage, {
    	
    name:null,
    version:null,
    description:null,
    size:null,
    layer:null,
    	
    initialize: function(name, version, description, size, layer) {
    	if (localStorage) {    	  
    	  this.name = name;
		  this.version = version;
		  this.description = description;
		  this.size = size;
		  this.layer = layer;
    	} else {
    		throw new Error("Local storage is not supported.");
    	}
		return this;
	},
	set: function(key, value) {
		try {
			localStorage.setItem(key,value);
		} catch (e) { 
			if((e.name).toUpperCase() == 'QUOTA_EXCEEDED_ERR') {
			  //TODO Replace with correct error handling.
			  alert ("Unable to cache area, cache is full.");
			}
		}
	},
	get: function(key, imgDiv) {		
		var value = localStorage.getItem(key);
	    if (value) { imgDiv.src = value; }
		return value;
	},
	clear: function() {
		localStorage.clear();
	},
	count: function(resultHandler) {		
		resultHandler("LocalStorage", localStorage.length);
	},

  CLASS_NAME: "OpenLayers.LocalTileStorage.TileWebStorage"
  });